define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kzvote/log/index' + location.search,
                    add_url: 'kzvote/log/add',
                    edit_url: 'kzvote/log/edit',
                    // del_url: 'kzvote/log/del',
                    multi_url: 'kzvote/log/multi',
                    import_url: 'kzvote/log/import',
                    table: 'kzvote_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: true,//默认显示搜索表单
                search: false,//默认显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), visible: false, operate: false},
                        {field: 'user_id', title: __('User_id'), visible: false, operate: false},
                        {field: 'vote_id', title: __('投票活动ID'), visible: false},
                        {field: 'vote.name', title: __('投票活动'), operate: 'LIKE'},
                        {field: 'person_id', title: __('作品ID'), visible: false},
                        {field: 'person.name', title: __('作品'), operate: 'LIKE'},
                        {field: 'ip', title: __('投票ip')},
                        {
                            field: 'type',
                            title: __('投票方式'),
                            searchList: {1: __('Type 1'), 2: __('Type 2'), 3: __('Type 3')},
                            formatter: Table.api.formatter.normal
                        },
                        // {field: 'num', title: __('Num')},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('投票表单'),
                                    text: '投票表单',
                                    icon: 'fa fa-th-large',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    visible: function (row) {
                                        if (row.join_form){
                                            return true;
                                        }
                                    }
                                }
                            ], formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //导出数据
            exports(Table, table)
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function exports(Table, table) {
//*************************** 自定义export开始
    var submitForm = function (ids, layero) {
        var options = table.bootstrapTable('getOptions');
        var columns = [];
        $.each(options.columns[0], function (i, j) {
            if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                columns.push(j.field);
            }
        });
        var search = options.queryParams({});
        $("input[name=search]", layero).val(options.searchText);
        $("input[name=ids]", layero).val(ids);
        $("input[name=filter]", layero).val(search.filter);
        $("input[name=op]", layero).val(search.op);
        $("input[name=columns]", layero).val(columns.join(','));
        $("form", layero).submit();
    };
    $(document).on("click", ".btn-export", function () {
        var ids = Table.api.selectedids(table);
        var page = table.bootstrapTable('getData');
        var all = table.bootstrapTable('getOptions').totalRows;
        Layer.confirm("是否确定导出<form action='log/export' method='post' target='_blank'>" +
            "<input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'>" +
            "<input type='hidden' name='search'><input type='hidden' name='columns'></form>", {

            title: '导出数据',
            btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
            success: function (layero, index) {
                $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
            },
            yes: function (index, layero) {
                layer.close(layer.index);
                submitForm(ids.join(","), layero);
                return false;
            },
            btn2: function (index, layero) {
                layer.close(layer.index);
                var ids = [];
                $.each(page, function (i, j) {
                    ids.push(j.id);
                });
                submitForm(ids.join(","), layero);
                return false;
            },
            btn3: function (index, layero) {
                layer.close(layer.index);
                submitForm("all", layero);
                return false;
            }
        });
    });
    //*************************** 自定义export结束
}